package com.basic.library.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.basic.library.R;
import com.maning.mndialoglibrary.config.MDialogConfig;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author yj
 * @description
 */
public class MYStatusDialog {

    private Context mContext;
    private Dialog mDialog;

    private MDialogConfig mDialogConfig;

    private TextView tvShow, tv_cancel, tv_confirm;

    public MYStatusDialog(Context context) {
        this(context, new MDialogConfig.Builder().build());
    }

    public MYStatusDialog(Context context, MDialogConfig dialogConfig) {
        mContext = context;
        mDialogConfig = dialogConfig;
        //初始化
        initDialog();
    }

    private void initDialog() {

        LayoutInflater inflater = LayoutInflater.from(mContext);
        View mProgressDialogView = inflater.inflate(R.layout.dialog_status, null);// 得到加载view
        mDialog = new Dialog(mContext);// 创建自定义样式dialog
        mDialog.setCancelable(false);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setContentView(mProgressDialogView);

        //设置整个Dialog的宽高
        Resources resources = mContext.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        int screenW = dm.widthPixels;
        int screenH = dm.heightPixels;

        WindowManager.LayoutParams layoutParams = mDialog.getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mDialog.getWindow().setAttributes(layoutParams);

        //获取布局
        tvShow = (TextView) mProgressDialogView.findViewById(R.id.tvShow);
        tvShow.setTextIsSelectable(true);
        tv_cancel = (TextView) mProgressDialogView.findViewById(R.id.tv_cancel);
        tv_confirm = (TextView) mProgressDialogView.findViewById(R.id.tv_confirm);

        //默认配置
        configView();

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String title = tvShow.getText().toString();
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                if (title.contains("【") && title.contains("】")) {
                    String text = title.substring(title.indexOf("【") + 1, title.lastIndexOf("】"));
                    ClipData mClipData = ClipData.newPlainText("设备码", text);
                    // 将ClipData内容放到系统剪贴板里。
                    cm.setPrimaryClip(mClipData);
                }
                dismiss();
            }
        });
    }

    private void configView() {
        if (mDialogConfig == null) {
            mDialogConfig = new MDialogConfig.Builder().build();
        }
        mDialog.setCancelable(mDialogConfig.cancelable);
        mDialog.setCanceledOnTouchOutside(false);
    }

    public void show(String msg, Drawable drawable) {
        show(msg, drawable, 2000);
    }

    @SuppressLint("CheckResult")
    public void show(String msg, Drawable drawable, long delayMillis) {
        tvShow.setText(msg);
        mDialog.show();
        Observable.timer(delayMillis, TimeUnit.SECONDS)
                .compose(((RxAppCompatActivity) mContext).bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> {
                    dismiss();
                });
    }

    public void dismiss() {
        try {
            mContext = null;
            if (mDialog != null) {
                mDialog.dismiss();
                mDialog = null;
            }
            if (mDialogConfig != null && mDialogConfig.onDialogDismissListener != null) {
                mDialogConfig.onDialogDismissListener.onDismiss();
            }
        } catch (Exception e) {

        }
    }
}
